package com.leo;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.entity.components.BoundingBoxComponent;
import com.almasb.fxgl.entity.components.IDComponent;
import com.almasb.fxgl.entity.components.TransformComponent;
import com.almasb.fxgl.entity.components.TypeComponent;
import com.almasb.fxgl.entity.components.ViewComponent;
import com.leo.entity.GameEntityFactory;
import javafx.scene.paint.Color;

import java.util.Optional;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-01  23:13
 * @Description: TODO
 */
public class EntityApp extends GameApplication {
    @Override
    protected void initSettings(GameSettings settings) {

    }

    @Override
    protected void initGame() {
        // fxgl  entity component system = ECS
        FXGL.getGameWorld().addEntityFactory(new GameEntityFactory());
        // spawn方法会创建实体并添加到游戏世界
        Entity e1 = FXGL.spawn("rect", new SpawnData().put("h", 100d).put("w", 100d).put("color", Color.RED));
        Entity e2 = FXGL.spawn("rect", new SpawnData(100, 100).put("h", 100d).put("w", 100d).put("color", Color.GREEN));

        // 计算两个实体之间的坐标距离，左上角
        System.out.println("distance:" + e1.distance(e2));
        // 计算两个实体bbox之间的距离，有交集或者包含关系那么距离就是 0
        System.out.println("distanceBBox:" + e1.distanceBBox(e2));

        // 实体只有被添加进游戏世界当中的时候才是活跃的
        System.out.println("实体 e1 是否活跃：" + e1.isActive());

        // 只是被创建
        Entity e3 = FXGL.getGameWorld().create("rect", new SpawnData(300, 300).put("h", 100d).put("w", 100d).put("color", Color.GRAY));
        System.out.println("实体 e3 仅仅被创建：" + e3.isActive());

        e3.setOnActive(() -> System.out.println("e3活跃了"));
        e3.setOnNotActive(() -> System.out.println("e3不活跃了"));

        FXGL.getGameWorld().addEntity(e3);
        System.out.println("实体 e3 被添加进游戏世界：" + e3.isActive());
//        FXGL.getGameWorld().removeEntity(e3);
        e3.removeFromWorld();
        System.out.println("实体 e3 从游戏世界移除：" + e3.isActive());

        // 实体是否有某个组件
        // TypeComponent 为fxgl四大核心组件，有没有设置都会存在
        boolean b = e1.hasComponent(TypeComponent.class);
        System.out.println(b ? "有type组件" : "没有type组件");

        // 四大核心组件
        e1.getComponents().forEach(System.out::println);
        // 获取组件的快捷方式
        // 指定类型
        TypeComponent typeComponent = e1.getTypeComponent();
        // 指定大小
        BoundingBoxComponent boundingBoxComponent = e1.getBoundingBoxComponent();
        // 指定外观
        ViewComponent viewComponent = e1.getViewComponent();
        // 指定位置
        TransformComponent transformComponent = e1.getTransformComponent();

        // 判断某个组件是否存在
        Optional<IDComponent> componentOptional = e1.getComponentOptional(IDComponent.class);
        if (componentOptional.isPresent()) {
            System.out.println("id组件存在");
        } else {
            System.out.println("id组件不存在");
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
